Skip to content

feat(starknet_os): verify syscall_ptr #6327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 11, 2025

Conversation

aner-starkware
Copy link
Contributor

No description provided.

@aner-starkware aner-starkware self-assigned this May 5, 2025
@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link

github-actions bot commented May 5, 2025

Benchmark movements:
full_committer_flow performance improved 😺
full_committer_flow time: [30.284 ms 30.329 ms 30.376 ms]
change: [-1.6225% -1.4208% -1.2037%] (p = 0.00 < 0.05)
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
5 (5.00%) high mild

@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch 3 times, most recently from 5786289 to b114059 Compare May 6, 2025 07:21
Copy link

github-actions bot commented May 6, 2025

Benchmark movements:
full_committer_flow performance improved 😺
full_committer_flow time: [30.126 ms 30.165 ms 30.206 ms]
change: [-2.1521% -1.7151% -1.3829%] (p = 0.00 < 0.05)
Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 9 files at r1.
Reviewable status: 3 of 9 files reviewed, 3 unresolved discussions (waiting on @aner-starkware)


a discussion (no related file):
I'd like to avoid (a) adding a new crate dependency just for pascal-casing and (b) avoid adding an extra field to the syscall processor.
instead, how about using the From<Felt> logic defined on the SyscallSelector?
see comments in your macro module on how I think this can work


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

                syscall_hint_processor.set_syscall_ptr(
                    get_ptr_from_var_name(Ids::SyscallPtr.into(), vm, ids_data, ap_tracking)?
                );

here, you can just fetch the integer at the current syscall pointer and verify it's the correct selector, no?
or is the pointer not pointing to the correct value at this point in time?
(see below, I'm assuming you have a $selector) macro ident available)

let syscall_ptr = get_ptr_from_var_name(Ids::SyscallPtr.into(), vm, ids_data, ap_tracking)?;
let selector = SyscallSelector::from(vm.get_integer(syscall_ptr)?)?;
if selector != SyscallSelector::$selector {
    return Err(..);
}
syscall_hint_processor.set_syscall_ptr(syscall_ptr);

Code quote:

                let syscall_hint_processor = &mut hint_processor.deprecated_syscall_hint_processor;
                syscall_hint_processor.set_syscall_name(
                    stringify!($name).to_string().to_case(Case::Pascal)
                );
                syscall_hint_processor.set_syscall_ptr(
                    get_ptr_from_var_name(Ids::SyscallPtr.into(), vm, ids_data, ap_tracking)?
                );

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r1 (raw file):

create_syscall_func!(
    call_contract,
    delegate_call,

... etc.
If paste has a pascal-case functionality you can use it, but if not, I wouldn't use an extra crate just to reduce the boilerplate here...

Suggestion:

create_syscall_func!(
    (call_contract, CallContract),
    (delegate_call, DelegateCall),

@aner-starkware
Copy link
Contributor Author

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

Previously, dorimedini-starkware wrote…

here, you can just fetch the integer at the current syscall pointer and verify it's the correct selector, no?
or is the pointer not pointing to the correct value at this point in time?
(see below, I'm assuming you have a $selector) macro ident available)

let syscall_ptr = get_ptr_from_var_name(Ids::SyscallPtr.into(), vm, ids_data, ap_tracking)?;
let selector = SyscallSelector::from(vm.get_integer(syscall_ptr)?)?;
if selector != SyscallSelector::$selector {
    return Err(..);
}
syscall_hint_processor.set_syscall_ptr(syscall_ptr);

I don't understand the suggestion - you're not using the $name. Am I missing something?

@aner-starkware
Copy link
Contributor Author

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

Previously, aner-starkware wrote…

I don't understand the suggestion - you're not using the $name. Am I missing something?

Ahhh, OK, $selector you meant $name , right?

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 9 files reviewed, 3 unresolved discussions (waiting on @aner-starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

Previously, aner-starkware wrote…

Ahhh, OK, $selector you meant $name , right?

in the verification area, I'm not. see below, instead of just the function name I'm passing the selector name (function name in pascal case).
alternatively, you can pass CallContract instead of call_contract, and then you can get the function name by using paste:

paste::paste! {
    fn [< $selector:snake >]( .. ) { .. }
}

@aner-starkware
Copy link
Contributor Author

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

Previously, dorimedini-starkware wrote…

in the verification area, I'm not. see below, instead of just the function name I'm passing the selector name (function name in pascal case).
alternatively, you can pass CallContract instead of call_contract, and then you can get the function name by using paste:

paste::paste! {
    fn [< $selector:snake >]( .. ) { .. }
}

OK, but in any case this solution checks it here, and not in verify_syscall_ptr - is this important?

@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch 2 times, most recently from 3d7683f to d86158c Compare May 8, 2025 08:38
@aner-starkware
Copy link
Contributor Author

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

Previously, aner-starkware wrote…

OK, but in any case this solution checks it here, and not in verify_syscall_ptr - is this important?

Additionally, the Err is on the pointers, but in this case we're comparing DeprecatedSyscallSelector - should I create a new Error type?

@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch from d86158c to bb5b22e Compare May 8, 2025 08:42
Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 9 files reviewed, 3 unresolved discussions (waiting on @aner-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

OK, but in any case this solution checks it here, and not in verify_syscall_ptr - is this important?

this is a good question; can you check that it is equivalent? if the pointer doesn't change between this call and the verify call I think it's fine, but @Yoni-Starkware 's or @ilyalesokhin-starkware 's input may help

should I create a new Error type?

I am in favor, yes

@aner-starkware
Copy link
Contributor Author

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r1 (raw file):

Previously, dorimedini-starkware wrote…

... etc.
If paste has a pascal-case functionality you can use it, but if not, I wouldn't use an extra crate just to reduce the boilerplate here...

paste has it (called camel if it's not confusing enough 🙄 )

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 9 files reviewed, 3 unresolved discussions (waiting on @aner-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r1 (raw file):

Previously, aner-starkware wrote…

paste has it (called camel if it's not confusing enough 🙄 )

camel makes the first letter uppercase??

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 9 of 9 files at r2, all commit messages.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @aner-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 52 at r2 (raw file):

                let syscall_selector = DeprecatedSyscallSelector::try_from(
                    felt_from_ptr(vm, &mut syscall_ptr)?
                ).unwrap();
  1. no unwrap in business logic - if this cannot fail, use expect, with a string explaining why it cannot fail. if it can fail, return an error
  2. does felt_from_ptr dereference the pointer? or just converts the address to felt?

Code quote:

                let syscall_selector = DeprecatedSyscallSelector::try_from(
                    felt_from_ptr(vm, &mut syscall_ptr)?
                ).unwrap();

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 53 at r2 (raw file):

                    felt_from_ptr(vm, &mut syscall_ptr)?
                ).unwrap();
                // TODO(Aner): should return an error instead of panic.

no reason to even merge it like this temporarily: add an error variant in the same PR, no problem (better, because it's in context)

Code quote:

// TODO(Aner): should return an error instead of panic.

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r2 (raw file):

                syscall_hint_processor.set_syscall_ptr(
                    syscall_ptr,
                );

format

Suggestion:

                paste!{
                    assert_eq!(syscall_selector, DeprecatedSyscallSelector::[<$name:camel>]);
                }
                syscall_hint_processor.set_syscall_ptr(syscall_ptr);

@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch from bb5b22e to 7e4057a Compare May 8, 2025 11:59
@aner-starkware
Copy link
Contributor Author

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r1 (raw file):

Previously, dorimedini-starkware wrote…

camel makes the first letter uppercase??

Turns out 🙄 (well, not in the case crate, but in paste .....)

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r3, all commit messages.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @aner-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)


crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs line 110 at r3 (raw file):

    },
    #[error("Bad syscall_ptr; expected: {expected_ptr:?}, got: {actual_ptr:?}.")]
    BadSyscallPointer { expected_ptr: Relocatable, actual_ptr: Relocatable },

Suggestion:

    #[error("Bad syscall_ptr; expected: {expected_ptr:?}, got: {actual_ptr:?}.")]
    BadSyscallPointer { expected_ptr: Relocatable, actual_ptr: Relocatable },
    #[error("Bad syscall selector; expected: {expected_selector:?}, got: {actual_selector:?}.")]
    BadSyscallSelector {
        expected_selector: DeprecatedSyscallSelector,
        actual_selector: DeprecatedSyscallSelector,
    },

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 55 at r3 (raw file):

                    felt_from_ptr(vm, &mut syscall_ptr)?
                ).unwrap();
                // TODO(Aner): should return an error instead of panic.

delete

Code quote:

// TODO(Aner): should return an error instead of panic.

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 63 at r3 (raw file):

                        }));
                    }
                }

non blocking, but consider doing this: that way you only need the hacky pasteing once, and less indentation

Suggestion:

                let expected_selector = paste! { DeprecatedSyscallSelector::[<$name:camel>] };
                if syscall_selector != expected_selector {
                    return Err(DeprecatedSyscallExecution(DeprecatedSyscallExecutionError::BadSyscallSelector {
                        expected_selector,
                        actual_selector: syscall_selector,
                    }));
                }
        

@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch 3 times, most recently from ec6f219 to 1e3ed19 Compare May 8, 2025 12:16
Copy link
Contributor Author

@aner-starkware aner-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 8 of 10 files reviewed, 6 unresolved discussions (waiting on @dorimedini-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 43 at r1 (raw file):

Previously, dorimedini-starkware wrote…

OK, but in any case this solution checks it here, and not in verify_syscall_ptr - is this important?

this is a good question; can you check that it is equivalent? if the pointer doesn't change between this call and the verify call I think it's fine, but @Yoni-Starkware 's or @ilyalesokhin-starkware 's input may help

should I create a new Error type?

I am in favor, yes

  1. The pointer doesn't change, and
  2. The pointer is also saved to self (and later compared with the same pointer 🙄 )

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 52 at r2 (raw file):

Previously, dorimedini-starkware wrote…
  1. no unwrap in business logic - if this cannot fail, use expect, with a string explaining why it cannot fail. if it can fail, return an error
  2. does felt_from_ptr dereference the pointer? or just converts the address to felt?

Good catch! Thanks 🙏


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 53 at r2 (raw file):

Previously, dorimedini-starkware wrote…

no reason to even merge it like this temporarily: add an error variant in the same PR, no problem (better, because it's in context)

Yeah, it was a temporary solution while I waited for your answer regarding the error.


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r2 (raw file):

Previously, dorimedini-starkware wrote…

format

Done.


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 55 at r3 (raw file):

Previously, dorimedini-starkware wrote…

delete

Done.


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 63 at r3 (raw file):

Previously, dorimedini-starkware wrote…

non blocking, but consider doing this: that way you only need the hacky pasteing once, and less indentation

Suggestion taken.


crates/blockifier/src/execution/deprecated_syscalls/hint_processor.rs line 110 at r3 (raw file):

    },
    #[error("Bad syscall_ptr; expected: {expected_ptr:?}, got: {actual_ptr:?}.")]
    BadSyscallPointer { expected_ptr: Relocatable, actual_ptr: Relocatable },

Done.

Copy link
Contributor Author

@aner-starkware aner-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 8 of 10 files reviewed, 6 unresolved discussions (waiting on @dorimedini-starkware, @ilyalesokhin-starkware, and @Yoni-Starkware)


a discussion (no related file):

Previously, dorimedini-starkware wrote…

I'd like to avoid (a) adding a new crate dependency just for pascal-casing and (b) avoid adding an extra field to the syscall processor.
instead, how about using the From<Felt> logic defined on the SyscallSelector?
see comments in your macro module on how I think this can work

Done.

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @aner-starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r2 (raw file):

Previously, aner-starkware wrote…

Done.

the set_syscall_ptr line is still 3 lines instead of 1


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 67 at r4 (raw file):

                        );
                    }
                }

you don't need the paste here anymore

Code quote:

                paste!{
                    if syscall_selector != expected_selector {
                        return Err(
                            DeprecatedSyscallExecution(
                                DeprecatedSyscallExecutionError::BadSyscallSelector {
                                    expected_selector,
                                    actual_selector: syscall_selector,
                                }
                            )
                        );
                    }
                }

@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch from 1e3ed19 to 5e85391 Compare May 8, 2025 12:52
Copy link
Contributor Author

@aner-starkware aner-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 9 of 10 files reviewed, 2 unresolved discussions (waiting on @dorimedini-starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 59 at r2 (raw file):

Previously, dorimedini-starkware wrote…

the set_syscall_ptr line is still 3 lines instead of 1

Done.


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 67 at r4 (raw file):

Previously, dorimedini-starkware wrote…

you don't need the paste here anymore

Done.

@aner-starkware aner-starkware changed the title WIP: feat(starknet_os): verify syscall_ptr feat(starknet_os): verify syscall_ptr May 8, 2025
Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r5, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @aner-starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 9 at r5 (raw file):

use paste::paste;

use crate::hints::error::OsHintError::DeprecatedSyscallExecution;

to not use enum variants please

Suggestion:

use crate::hints::error::OsHintError;

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 30 at r5 (raw file):

///     let syscall_hint_processor = &mut hint_processor.deprecated_syscall_hint_processor;
///     Ok(execute_next_deprecated_syscall(syscall_hint_processor, vm, ids_data, ap_tracking)?)
/// }

this docstring is no longer true

Code quote:

/// create_syscall_func!(get_block_number);
///
/// Expands to:
///
/// pub(crate) fn get_block_number<S: StateReader>(
/// HintArgs { hint_processor, vm, ids_data, ap_tracking, .. }: HintArgs<'_, '_, S>,
/// ) -> OsHintResult
/// {
///     let syscall_hint_processor = &mut hint_processor.deprecated_syscall_hint_processor;
///     Ok(execute_next_deprecated_syscall(syscall_hint_processor, vm, ids_data, ap_tracking)?)
/// }

crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 64 at r5 (raw file):

                            }
                        )
                    );

this should work, you have a #[from] implemented

Suggestion:

                    return Err(
                        DeprecatedSyscallExecutionError::BadSyscallSelector {
                            expected_selector,
                            actual_selector: syscall_selector,
                        }
                        .into()
                    );

Copy link
Contributor Author

@aner-starkware aner-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @dorimedini-starkware)


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 9 at r5 (raw file):

Previously, dorimedini-starkware wrote…

to not use enum variants please

Done.


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 30 at r5 (raw file):

Previously, dorimedini-starkware wrote…

this docstring is no longer true

Done.


crates/starknet_os/src/hints/hint_implementation/syscalls.rs line 64 at r5 (raw file):

Previously, dorimedini-starkware wrote…

this should work, you have a #[from] implemented

Done.

Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @aner-starkware)

@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch from 9ad17e9 to 930e077 Compare May 11, 2025 11:05
@aner-starkware aner-starkware force-pushed the aner/snos_verify_syscall_ptr branch from 930e077 to fb367ac Compare May 11, 2025 11:35
Copy link
Collaborator

@dorimedini-starkware dorimedini-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r8, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @aner-starkware)

@aner-starkware aner-starkware added this pull request to the merge queue May 11, 2025
Merged via the queue into main with commit 5c5e31b May 11, 2025
17 checks passed
@aner-starkware aner-starkware deleted the aner/snos_verify_syscall_ptr branch May 11, 2025 14:26
@github-actions github-actions bot locked and limited conversation to collaborators May 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants